package main;

import java.util.ArrayList;

/**
 * 
 * @author Wojciech Stolarz
 * @version 1.0 18-12-2011
 */
public class Its {
	private String name;
	private int id;
	private static int counter = 0;
	private ArrayList<Problem> problems = new ArrayList<Problem>();

	public Its(String name) {
		this.id = counter++;
		this.setName(name);
	}
	
	public int getId(){
		return id;
	}

	public ArrayList<Problem> getProblems() {
		return problems;
	}

	public void setProblems(ArrayList<Problem> problems) {
		this.problems = problems;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void addProblem(String name) {
		int idx = getProblemIdx(name);
		if(idx == -1) {
			problems.add(new Problem(name));
		}
	}

	public void removeProblem(String name) {
		int idx = -2;
		if ((idx = getProblemIdx(name)) >= 0) {
			problems.remove(idx);
		}
	}

	public int getProblemIdx(String name) {
		Problem tmp = new Problem(name);
		int idx = problems.indexOf(tmp);
		return idx;
	}

	public boolean addSubProblem(String probName, String subName, double perf) {
		boolean result = false;
		int idx = getProblemIdx(probName);
		if (idx >= 0) {
			problems.get(idx).addTrait(subName, perf);
			result = true;
		}

		return result;
	}

	public void remSubProblem(String probName, String subName) {
		int idx = -2;
		if ((idx = getProblemIdx(probName)) >= 0) {
			problems.get(idx).removeTrait(subName);
		}
	}

	public boolean updateSubProblem(String probName, String subName, double perf) {
		boolean result = false;
		int idx = -2;
		if ((idx = getProblemIdx(probName)) >= 0) {
			problems.get(idx).updateTrait(subName, perf);
		}

		return result;
	}

	public double getProblemPerf(String name) {
		double perf = 0.0;
		int idx = -2;
		if ((idx = getProblemIdx(name)) >= 0) {
			perf = problems.get(idx).getProlemPerf();
		}
		return perf;
	}

	public String toString() {
		String tmp = getName() + "\n";
		for (Problem p : problems) {
			tmp += p + "\n";
		}
		return tmp;
	}

	class Problem {
		private String name;
		private ArrayList<Trait> traits = new ArrayList<Trait>();

		public String getName() {
			return name;
		}

		public double getProlemPerf() {
			double overall = 0.0;
			for (Trait t : traits) {
				overall += t.getPerformance();
			}
			return overall;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Problem(String name) {
			this.name = name;
		}

		public ArrayList<Trait> getTraits() {
			return traits;
		}

		public double getTraitPerf(String infl) {
			Trait tmp = new Trait(infl, 0.0);
			if (traits.contains(tmp)) {
				int idx = traits.indexOf(tmp);
				return traits.get(idx).getPerformance();
			}
			return 0.0;
		}

		public void addTrait(String infl, double perf) {
			Trait trait = new Trait(infl, perf);
			traits.add(trait);
		}

		public boolean removeTrait(String infl) {
			Trait tmp = new Trait(infl, 0.0);
			if (traits.contains(tmp)) {
				int idx = traits.indexOf(tmp);
				traits.remove(idx);
				return true;
			}
			return false;
		}

		public boolean updateTrait(String infl, double perf) {
			Trait tmp = new Trait(infl, 0.0);
			if (traits.contains(tmp)) {
				int idx = traits.indexOf(tmp);
				traits.get(idx).setPerformance(perf);
				return true;
			}
			return false;
		}

		public boolean equals(Object o) {
			Problem tmp = (Problem) o;
			if (this.getName().equalsIgnoreCase(tmp.getName()))
				return true;
			return false;
		}

		public String toString() {
			String tmp = getName() + "\n";
			for (Trait t : traits) {
				tmp += t.getInfluence() + ": " + t.getPerformance();
				tmp += "\n";
			}
			return tmp;
		}

		class Trait {
			private double performance;
			private String influence;

			public Trait(String influence, double performance) {
				super();
				this.performance = performance;
				this.influence = influence;
			}

			public double getPerformance() {
				return performance;
			}

			public void setPerformance(double performance) {
				this.performance = performance;
			}

			public String getInfluence() {
				return influence;
			}

			public void setInfluence(String influence) {
				this.influence = influence;
			}

			public boolean equals(Object o) {
				Trait tmp = (Trait) o;
				if (this.influence.equalsIgnoreCase(tmp.influence))
					return true;
				return false;
			}
		}
	}
}
